﻿using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using CMS.Areas.EasyUI.Controllers;
using Service.Admin;
using Service.Content;
using Service.EasyUI;
using Service.Manager;
using Service.Permission;

namespace CMS
{
    /// <summary>
    /// MvcApplication
    /// </summary>
    public class MvcApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// Application_Start
        /// </summary>
        protected void Application_Start()
        {
            InitContainer();

            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        /// <summary>
        /// 初始化Container
        /// </summary>
        private void InitContainer()
        {
            //创建autofac管理注册类的容器实例
            var builder = new ContainerBuilder();
            //为容器注册可管理的类型
            SetupResolveRules(builder);
            //使用Autofac提供的RegisterControllers扩展方法来对程序集中所有的Controller一次性的完成注册
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            //生成具体的实例
            var container = builder.Build();
            //使用MVC的扩展 更改了MVC中的注入方式
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

        /// <summary>
        /// 注册相关类型
        /// </summary>
        /// <param name="builder"></param>
        private void SetupResolveRules(ContainerBuilder builder)
        { 
            builder.RegisterType<UserInfoService>().As<UserInfoService>();
            builder.RegisterType<UserMenuService>().As<UserMenuService>();
            builder.RegisterType<AreasService>().As<AreasService>();
            builder.RegisterType<CategoryService>().As<CategoryService>();
            builder.RegisterType<ProductService>().As<ProductService>();
            builder.RegisterType<UserActionService>().As<UserActionService>();
            builder.RegisterType<ArticleService>().As<ArticleService>();
            builder.RegisterType<UserTypeService>().As<UserTypeService>();
            builder.RegisterType<MenuService>().As<MenuService>();
        }
    }
}
